package c.x.jy.service.service.impl;

import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.Query;
import c.x.jy.dao.dao.CmsArticleDao;
import c.x.jy.dao.entity.CmsArticleEntity;
import c.x.jy.service.service.CmsArticleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("cmsArticleService")
public class CmsArticleServiceImpl extends ServiceImpl<CmsArticleDao, CmsArticleEntity> implements CmsArticleService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        	QueryWrapper<CmsArticleEntity> query= 	 new QueryWrapper<CmsArticleEntity>() ;
    	if(params.get("searchKey")!=null&&params.get("searchValue")!=null&&!StringUtils.isEmpty(params.get("searchKey").toString())&&!StringUtils.isEmpty(params.get("searchValue").toString()))
    	    query.eq(  params.get("searchKey").toString(), params.get("searchValue"));
        if(params.get("businessId")!=null) 	query.eq("business_id", params.get("businessId"));
        IPage<CmsArticleEntity> page = this.page(
                new Query<CmsArticleEntity>().getPage(params),
                 query
        );

        return new PageUtils(page);
    }

    @Override
    public int updateHits(Long id) {

     return   this.baseMapper.updateHits(id);
    }

    @Override
    public IPage<CmsArticleEntity> selectList(Map<String, Object> params) {
        IPage<CmsArticleEntity> page=	 new Query<CmsArticleEntity>().getPage(params);

        QueryWrapper<CmsArticleEntity> query=new QueryWrapper<>();
        if(params.get("cmsCategoryId")!=null)
            query.like("category_ids", "\\\""+params.get("cmsCategoryId")+"\\\"");//("category_ids" )("category_ids", cmsCategoryId);
        if(params.get("cmsKey")!=null)
            query.like("title", params.get("cmsKey"));//("category_ids" )("category_ids", cmsCategoryId);

        System.out.println("cmskey="+params.get("cmsKey"));

        IPage<CmsArticleEntity> pages =this.baseMapper.selectPages(page ,query);
        return pages ;
    }

    @Override
    public CmsArticleEntity getArticleByID(Integer articleId) {
        if (articleId == null) return null;
        QueryWrapper<CmsArticleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CmsArticleEntity::getId,articleId);
        return this.baseMapper.selectOne(queryWrapper);

    }

    @Override
    public List<CmsArticleEntity> selectListforHome(String ids) {

        return this.baseMapper.selectListforHome(ids);
    }
}
